﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Data.BLL;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Questionnaire;
using ZHDJ.WebPage;

namespace SysManage.Module.Questionnaire
{
    public partial class QuestionnaireList : PageBase
    {
        public string Module
        {
            get
            {
                var m= GetQueryIntValue("Module");
                if (m == "")
                {
                    return BranchID;
                }
                return m;
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyQuestionnaireView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            // 权限检查
            CheckPowerWithButton("PartyQuestionnaireAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("PartyQuestionnaireEdit.aspx?Module="+ Module, "新增");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
        }
        private void LoadData()
        {
            var ds = PartyQuestionnaire.GetGridData(Grid1.PageIndex, Grid1.PageSize, ddlStatus.SelectedValue,
                txtTitle.Text);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyQuestionnaireEdit", Grid1, "editField");
            CheckPowerWithWindowField("PartyQuestionnaireEdit", Grid1, "editField2");
            CheckPowerWithLinkButtonField("PartyQuestionnaireDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyQuestionnaireId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyQuestionnaireDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyQuestionnaires.FirstOrDefault(t => t.ID == PartyQuestionnaireId);
                
                DB.PartyQuestionnaires.Remove(del);
                if (DB.SaveChanges() > 0)
                {
                    
                }
                LoadData();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }

        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyQuestionnaireDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);
            List<string> listLoginid = new List<string>();
            // 执行数据库操作
            DB.PartyQuestionnaires.Where(u => ids.Contains(u.ID)).ToList().ForEach(
                u =>
                {
                   
                    DB.PartyQuestionnaires.Remove(u);

                });
            if (DB.SaveChanges() > 0)
            {
              
            }

            // 重新绑定表格
            LoadData();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }
}